<template>
	<view>
		<view class="page_top">
			<u-navbar title="确定订单" leftIconColor="#fff" :titleStyle="{color:'#fff'}" :autoBack="true"
				:placeholder="true" :fixed="true" bgColor="transparent"></u-navbar>
			<view class="address flex" @click="go_address">
				<view class="address_left">
					<view class="address_left_top">
						{{address.address||'选择位置'}}
					</view>
					<view class="address_leftbottom">
						<text style="margin-right: 20rpx;">{{address.name||''}}</text> {{address.mobile||''}}
					</view>
				</view>
				<u-icon name="arrow-right" color="#333333" size="13"></u-icon>
			</view>
		</view>
		<view class="list" v-if="obj!=''">
			<view class="list_item">
				<view v-if="!isshop" class="list_item_top">
					{{obj.drug_shop||''}}
				</view>
				<view class="list_item_bottom">
					<view class="list_item_bottom_top flex">
						<image :src="ImgUrl+obj.img" class="list_item_bottom_top_img" mode=""></image>
						<view class="list_item_bottom_top_right">
							<view class="list_item_bottom_top_right_title line-1">
								{{obj.name}}
							</view>
							<view class="list_item_bottom_top_right_min">
								<text v-if="obj.is_prescription==1"
									class="list_item_bottom_top_right_min_icon">处方药</text>
								规格:{{obj.selectguige}}
							</view>
							<view class="price_view" style="color: #E5432E; align-items: baseline;">
								<template v-if="!isshop">
									<text class="fs-32">{{obj.sore}}</text>
									<text class="fs-24">积分</text>+
								</template>
								<text class="fs-24">¥</text>
								<text class="fs-32">{{obj.jiage | pointsPrice}}</text>
								<text class="fs-24">.{{obj.jiage | pointsPrice(1)}}</text>
							</view>
						</view>
					</view>
					<view class="mar20 flex">
						<view class="flex_left">
							配送费
						</view>
						<view class="flex_left">
							¥{{obj.fare}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="bottom flex">
			<view class="price_view fmar" style="color: #E5432E; align-items: baseline;">
				<template v-if="!isshop">
					<text class="fs-52">{{obj.sore*obj.num}}</text>
					<text class="fs-32">积分</text>
					<text class="fs-52">+</text>
				</template>
				<text class="fs-32">¥</text>
				<text class="fs-52">{{obj.jiage*obj.num | pointsPrice}}</text>
				<text class="fs-32">.{{obj.jiage*obj.num | pointsPrice(1)}}</text>
			</view>
			<view class="bottom_btn" @click="determine">
				提交
			</view>
		</view>
	</view>
</template>

<script>
	import {
		pointsPrice
	} from '@/utils/util1.js'
	import {
		address,
		shophandle,
		scorehandle,
		pays
	} from "@/api/common.js"
	export default {
		data() {
			return {
				address: '',
				obj: '',
				isshop:true,
			};
		},
		computed: {
			ImgUrl() {
				// console.log(this.$store.state.ImgUrl)
				return this.$store.state.ImgUrl
			}
		},
		filters: {
			pointsPrice
		},
		onLoad(options) {
			this.obj = JSON.parse(decodeURIComponent(options.obj));
			this.isshop = !this.obj.hasOwnProperty('sore');
		},
		onShow() {
			address().then(res => {
				console.log(res,'123');
				for (let i = 0; i < res.data.data.length; i++) {
					if (res.data.data[i].is_default == 1) {
						this.address = res.data.data[i];
					}
				}
				if (res.data.data.length != 0 && this.address == '') {
					this.address = res.data.data[0];
				}
				console.log(this.address,'1234');
			})
		},
		methods: {
			pay(type,sn){
				pays({
					type,
					order_sn:sn,
					channel:2
				}).then(res=>{
					let obj = res.data.payInfo;
					uni.requestPayment({
					    provider: 'wxpay',
						...obj,
						// timeStamp: obj.timeStamp,
						// nonceStr: obj.nonceStr,
						// package: obj.package,
						// signType: obj.signType,
						// paySign: obj.paySign,
						success: function (res) {
							console.log('success:' + JSON.stringify(res));
							uni.showToast({
								title: '支付成功',
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								})
							}, 800)
						},
						fail: function (err) {
							// console.log('fail:' + JSON.stringify(err));
							uni.showToast({
								title:'取消支付',
								icon:'none'
							})
						}
					});
				})
			},
			determine() {
				if (this.address == '') {
					uni.showToast({
						title: '请选择地址',
						icon: 'none'
					})
					return;
				}
				let data = {
					order: [{
						goods_id: this.obj.goods_id,
						sku_id: this.obj.sku_id,
						num: this.obj.num,
					}],
					address_id: this.address.id,
					isArray: 1
				}
				if(!this.isshop){
					// 积分商城
					console.log('sore')
					scorehandle(data).then(res => {
						console.log(res, 'cs');
						let {
							order_sn,
							user_id,
							amount,
							score,
							fare,
							total,
							remark,
							address_id,
							created_at,
							updated_at
						} = res.data;
						this.pay(3,order_sn);
					})
				}else{
					console.log('shop')
					// 商城
					shophandle(data).then(res=>{
						console.log(res);
						let {
							order_sn,
							user_id,
							amount,
							score,
							fare,
							total,
							remark,
							address_id,
							created_at,
							updated_at
						} = res.data;
						this.pay(2,order_sn);
						// if(this.obj.is_prescription==1){
						// 	uni.navigateTo({
						// 		url:'/pages/ConfirmOrder/SupplementConfirmOrder/SupplementConfirmOrder'
						// 	})
						// }else{
							
						// }
					})
					
				}
			},
			go_address() {
				uni.navigateTo({
					url: '/pages/MyAddress/MyAddress'
				})
			}
		}
	};
</script>

<style>
	page {
		background: #F4F4F4;
	}

	.page_top {
		height: 270rpx;
		background: linear-gradient(180deg, #0165FB 0%, rgba(1, 101, 251, 0.00) 100%);
	}

	.address {
		width: 702rpx;
		height: 146rpx;
		margin: 0 auto;
		border-radius: 20rpx;
		background: #FFF;
		margin-top: 30rpx;
		padding: 28rpx 30rpx;
		box-sizing: border-box;
	}

	.address_leftbottom {
		color: #666;
		font-family: Alibaba PuHuiTi 2.0;
		font-size: 24rpx;
		font-weight: 400;
		margin-top: 12rpx;
	}

	.address_left_top {
		color: #333;
		font-family: Alibaba PuHuiTi 2.0;
		font-size: 32rpx;
		font-weight: 700;
	}

	.flex {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.flex1 {
		display: flex;
		align-items: center;
	}

	.list {
		margin-top: 20rpx;
	}

	.list_item:first-child {
		margin-top: 0rpx;
	}

	.list_item {
		width: 702rpx;
		margin: 0 auto;
		margin-top: 20rpx;
		background: #fff;
		border-radius: 20rpx;
	}

	.list_item_top {
		width: 100%;
		padding: 30rpx;
		box-sizing: border-box;
		border-bottom: 1rpx solid #F4F4F4;
		;
	}

	.list_item_bottom {
		padding: 30rpx;
		box-sizing: border-box;
	}

	.list_item_bottom_top_img {
		width: 120rpx;
		height: 120rpx;
		border-radius: 8rpx;
		background: #ccc;
	}

	.list_item_bottom_top_right {
		width: calc(100% - 140rpx);

	}

	.list_item_bottom_top_right_title {
		color: #333;
		text-overflow: ellipsis;
		font-family: Alibaba PuHuiTi 2.0;
		font-size: 28rpx;
		font-weight: 400;
	}

	.list_item_bottom_top_right_min {
		color: #999;
		font-family: Alibaba PuHuiTi 2.0;
		font-size: 24rpx;
		font-weight: 400;
		margin-top: 4rpx;
	}

	.list_item_bottom_top_right_min_icon {
		display: inline-block;
		padding: 2rpx 8rpx;
		border-radius: 8rpx 8rpx 0rpx 8rpx;
		background: linear-gradient(239deg, #FCF1CF 31.36%, #FAE6A5 68.64%);
		color: #5F360F;
		font-family: Alibaba PuHuiTi 2.0;
		font-size: 22rpx;
		font-weight: 400;
		margin-right: 16rpx;
	}

	.price_view {
		margin-top: 6rpx;
	}

	.fs-32 {
		font-size: 32rpx;
	}

	.fs-24 {
		font-size: 24rpx;
	}

	.fs-32 {
		font-size: 32rpx;
	}

	.fs-52 {
		font-size: 52rpx;
	}

	.flex_left {
		color: #333;
		font-family: Alibaba PuHuiTi 2.0;
		font-size: 24rpx;
		font-weight: 400;
	}

	.mar20 {
		margin-top: 20rpx;
	}

	.bottom {
		width: 100%;
		height: 140rpx;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		display: flex;
		align-items: center;
		padding: 0 30rpx;
		box-sizing: border-box;
	}

	.bottom_btn {
		width: 300rpx;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		border-radius: 45rpx;
		background: #0165FB;
		margin-top: -15rpx;
		color: #FFF;
		text-align: center;
		font-family: Alibaba PuHuiTi 2.0;
		font-size: 30rpx;
		font-style: normal;
		font-weight: 400;
	}

	.fmar {
		margin-top: -15rpx;
	}
</style>